//
//  Progress.swift
//  Progress
//
//  Created by bin hu on 2025/6/25.
//

import UIKit

class RingProgressBar: UIView {
    // 1. 创建两个图层：背景环 + 进度环
    private let backgroundLayer = CAShapeLayer()
    private let progressLayer = CAShapeLayer()
    private let progressLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupLayers()
        setupLabel()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupLayers()
        setupLabel()
    }
    
    private func setupLayers() {
        // 2. 创建圆形路径
        let circularPath = UIBezierPath(
            arcCenter: CGPoint(x: bounds.midX, y: bounds.midY),
            radius: min(bounds.width, bounds.height)/2 - 10,
            startAngle: -CGFloat.pi/2,
            endAngle: 3*CGFloat.pi/2,
            clockwise: true
        )
        
        // 3. 配置背景环（剩余区域）
        backgroundLayer.path = circularPath.cgPath
        backgroundLayer.fillColor = UIColor.clear.cgColor
        backgroundLayer.strokeColor = UIColor.red.cgColor // 剩余区域颜色
        backgroundLayer.lineWidth = 12
        backgroundLayer.lineCap = .round
        backgroundLayer.strokeEnd = 1.0 // 始终保持完整环形
        layer.addSublayer(backgroundLayer)
        
        // 4. 配置进度环
        progressLayer.path = circularPath.cgPath
        progressLayer.fillColor = UIColor.clear.cgColor
        progressLayer.strokeColor = UIColor.green.cgColor // 进度区域颜色
        progressLayer.lineWidth = 12
        progressLayer.lineCap = .round
        progressLayer.strokeEnd = 0 // 初始进度为0
        layer.addSublayer(progressLayer)
    }
    
    private func setupLabel() {
        progressLabel.textAlignment = .center
        progressLabel.textColor = .darkGray
        progressLabel.font = .boldSystemFont(ofSize: 24)
        addSubview(progressLabel)
        
        progressLabel.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            progressLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
            progressLabel.centerYAnchor.constraint(equalTo: centerYAnchor)
        ])
    }
    
    // 5. 设置进度值（含动画）
    func setProgress(_ value: CGFloat, animated: Bool = true) {
        let clampedValue = min(max(value, 0), 1)
        
        if animated {
            let animation = CABasicAnimation(keyPath: "strokeEnd")
            animation.fromValue = progressLayer.strokeEnd
            animation.toValue = clampedValue
            animation.duration = 1.0
            animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
            progressLayer.strokeEnd = clampedValue
            progressLayer.add(animation, forKey: "progressAnimation")
        } else {
            progressLayer.strokeEnd = clampedValue
        }
        
        // 6. 更新标签文本
        let percentage = Int(clampedValue * 100)
        progressLabel.text = "\(percentage)%"
        
//        // 7. 动态改变进度条颜色（可选）
//        if clampedValue > 0.7 {
//            progressLayer.strokeColor = UIColor.systemGreen.cgColor
//        } else if clampedValue > 0.3 {
//            progressLayer.strokeColor = UIColor.systemYellow.cgColor
//        } else {
//            progressLayer.strokeColor = UIColor.systemRed.cgColor
//        }
    }
}



